package net.xinqushi.design.Factory;

import net.xinqushi.design.Factory.model.Bike;
import net.xinqushi.design.Factory.model.Car;
import net.xinqushi.design.Factory.model.TrafficRepairTools;
import net.xinqushi.design.Factory.model.Traffic;

import java.util.concurrent.Executors;

/**
 * 简单工厂 ，通过工厂方法获得实例 ，适合生产对象比较固定,不会变化,缺点扩展性不好，简单工厂不属于GOF23种设计模式
 * 如添加bean实现需要修改原有getBean方法代码。违反了 “开闭原则”(可以拓展，不可修改原有模块)
 */
public class SimpleTrafficFactory {

    public static Traffic getBean(String type){
        if(type.equals("bike")) return Bike.getInstance();
        if(type.equals("car")) return Car.getInstance();
        return null;
    }

}

class Main{
    public static void main(String[]args){
        Executors.newCachedThreadPool();
    }
}

/**
 *  静态工厂(简单工厂模式), 经常使用 . 例子： Executor
 */
class StaticTrafficFactory{
    static Traffic getCar(){return Car.getInstance();}

    static Traffic getBike(){return Bike.getInstance();}
}

/**
 * 工厂方法 , 生产同一种类产品,每个产品都有对应的工厂
 */

abstract class AbstractTrafficFactoryMethod{

    abstract Traffic getTraffic();

    static class DefaultFactory extends AbstractTrafficFactoryMethod {

        //默认工厂 为 Car工厂
        @Override
        Traffic getTraffic() {
            return new CarFactory().getTraffic();
        }
    }

    static class CarFactory extends AbstractTrafficFactoryMethod {

        @Override
        Traffic getTraffic() {
            return Car.getInstance();
        }
    }

    static class BikeFactory extends AbstractTrafficFactoryMethod {

        @Override
        Traffic getTraffic() {
            return Bike.getInstance();
        }
    }
}

/**
 * 抽象工厂 , 生产不同种类产品,每个产品都有对应的工厂
 * 当产品只有一类时，抽象工厂=工厂方法
 */
abstract class AbstractTrafficFactory{

    abstract Traffic getTraffic();

    abstract TrafficRepairTools getRepairTools();

    static class DefaultFactory extends AbstractTrafficFactory {

        //默认工厂 为 Car工厂
        @Override
        Traffic getTraffic() {
            return new CarFactory().getTraffic();
        }

        @Override
        TrafficRepairTools getRepairTools() {
            return null;
        }
    }

    static class CarFactory extends AbstractTrafficFactory {

        @Override
        Traffic getTraffic() {
            return Car.getInstance();
        }

        @Override
        TrafficRepairTools getRepairTools() {
            return null;
        }
    }

    static class BikeFactory extends AbstractTrafficFactory {

        @Override
        Traffic getTraffic() {
            return Bike.getInstance();
        }

        @Override
        TrafficRepairTools getRepairTools() {
            return null;
        }
    }
}

